package ljt.music.ui.playlist;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.ljq.mvpframework.presenter.BaseMvpPresenter;

import java.util.List;

import ljt.music.bean.PlayList;
import ljt.music.bean.SreachBean;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author jiangtao
 * @date 2017/12/21
 */

public class PlayListPresenter extends BaseMvpPresenter<PlayListView> {
  private PlayListModel mPlayListModel;

  public PlayListPresenter() {
    this.mPlayListModel = new PlayListModel();
  }

  public void request(final String id, final String types) {
    Log.e("perfect-mvp", "点击事件 P ");
    if (getMvpView() != null) {
      getMvpView().requestLoading();
    }

    mPlayListModel.request(id, types, new Callback<PlayList>() {
      @Override
      public void onResponse(Call<PlayList> call, Response<PlayList> response) {
        if (response.isSuccessful()) {
          Log.i("TAG onResponse", response.body().toString());
          getMvpView().resultSuccess(response.body());
        }
      }

      @Override
      public void onFailure(Call<PlayList> call, Throwable t) {
        Log.i("TAG onFailure", t.toString());
      }
    });
  }

  public void search(String types, String count, String source, String pages, String name) {
    Log.e("perfect-mvp", "点击事件 P ");
    if (getMvpView() != null) {
      getMvpView().requestLoading();
    }

    mPlayListModel.search(types,count,source,pages, name, new Callback<List<SreachBean>>() {
      @Override
      public void onResponse(Call<List<SreachBean>> call, Response<List<SreachBean>> response) {
        if (response.isSuccessful()) {
          getMvpView().searchSuccess(response.body());
        }
      }

      @Override
      public void onFailure(Call<List<SreachBean>> call, Throwable t) {
        Log.i("TAG", t.toString());
      }
    });
  }

  @Override
  public void onCreatePresenter(@Nullable Bundle savedState) {
    super.onCreatePresenter(savedState);
    if (savedState != null) {
      Log.e("perfect-mvp", "MainActivityPresenter  onCreatePresenter 测试  = " + savedState.getString("test2"));
    }
  }

  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Log.e("perfect-mvp", "MainActivityPresenter  onSaveInstanceState 测试 ");
    outState.putString("test2", "test_save2");
  }

  @Override
  public void onDestroyPresenter() {
    super.onDestroyPresenter();
  }

  public void interruptHttp() {
    mPlayListModel.interruptHttp();
  }
}
